#!/usr/bin/env python
import MySQLdb

class SQL:
    def __init__(self, dname, tname):
        self.conn = MySQLdb.connect( host = "localhost", user = "ssp", passwd =
                "ssp", db = dname)
        self.cursor = self.conn.cursor()
        self.tn = tname

    def free(self):
        self.cursor.close()
        self.conn.close()

    def tpl_sql(self, cmd):
        command = "select * from" + self.tn + "where "+cmd 
        self.cursor.execute(command)
        result = []
        while(1):
            row = self.cursor.fetchone()
            if row == None:
                break
            result.append(row)
        return result

    def root(self):
        command = " type = 'bind_expr' "
        return self.tpl_sql(command)
    
    def current(self, addr):
        command = " addr = " + str(addr)
        return self.tpl_sql(command)

    def father(self, addr):
        command = " op0 = " + str(addr) + " or op1 = "\
                + str(addr) + " or op2 = " + str(addr)
        return self.tpl_sql(command)
    
    def parm_list(self):
        command = " type = 'parm_decl' "
        return self.tpl_sql(command)

class NODE:
    def __init__(self, data, sql):
            self.addr = data[1]
            self.nodetype = data[2]
            self.vartype = data[8]
            self.number = 0
            if data[6] == 'NULL':
                self.name = None
            else:
                self.name = data[6]
            self.children = []
            if data[3] != 0:
                RAW = sql.addr(data[3])
                for raw in RAW:
                    if raw != None:
                        op = NODE ( raw, sql )
                        self.add_child(op)

            if data[4] != 0:
                RAW = sql.addr(data[4])
                for raw in RAW:
                    if raw != None:
                        op = NODE ( raw, sql )
                        self.add_child(op)

            if data[5] != 0:
                RAW = sql.addr(data[5])
                for raw in RAW:
                    if raw != None:
                        op = NODE ( raw, sql )
                        self.add_child(op)


    def add_child(self, node):
        if node != None:
            self.children.append(node)

    def dump(self):
        print "addr: \t", self.addr
        print "node: \t", self.nodetype
        print "type: \t", self.vartype
        print "name: \t", self.name
        print "child:\t", self.children
        print ""

    def walk(self):
        self.dump()
        for x in self.children:
            x.walk()

class AST:
    def __init__(self, raw):
        self.addr = raw[1]
        self.ntype = raw[2]
        self.child = []
        if raw[3] != None:
            self.child.append(raw[3])
        if raw[4] != None:
            self.child.append(raw[4])
        if raw[5] != None:
            self.child.append(raw[5])
        self.name = raw[6]
        self.no = raw[7]
        self.vtype = raw[8]
        self.value = raw[9]

    def debug(self):
        print "addr: \t", self.addr
        print "node: \t", self.ntype
        print "type: \t", self.vtype
        print "name: \t", self.name
        print "child:\t", self.child
        print "value:\t", self.value

#below are test
sql = SQL( "test", "`filter@filter`" )
root = sql.root()
parm = sql.parm_list()
for x in parm:
    X = AST(x)
    X.debug()
    for y in sql.father(X.addr):
        print y



sql.free()
